package cn.lizemin.fakedata.utils;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author lzm
 * @Date 2024/7/14
 * @description
 */
@Slf4j
public class SecurityUtil {

    private static final List<String> IGNORE_PATTERNS = Lists.newArrayList("/**/login"
            , "/**/logout");

    /**
     * 是否放行请求
     * @param req
     * @return true: 放行请求
     */
    public static boolean validateRequest(HttpServletRequest req) {
        String path = req.getServletPath();
        log.info("用户请求的接口路径为：{}", path);
        AntPathMatcher matcher = new AntPathMatcher();
        for (String pattern : IGNORE_PATTERNS) {
            if (matcher.match(pattern, path)) {
                return true;
            }
        }
        return false;
    }

    public static HttpServletResponse toHttpServletResponse(ServletResponse resp) {
        HttpServletResponse response = (HttpServletResponse) resp;
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setContentType("text/html;charset=utf-8");
        return response;
    }

    public static HttpServletRequest toHttpServletRequest(ServletRequest req) {
        return (HttpServletRequest) req;
    }


}
